package com.yesep.learn.design.behavioral.chain_of_responsibility.impl.leave;

import com.yesep.learn.design.behavioral.chain_of_responsibility.engine.*;

/**
 * 请假责任链构建对象
 */
public class LeaveChainBuilder implements IChainBuilder<LeaveRequest> {
    private final LeaveRequest request;

    public LeaveChainBuilder(LeaveRequest request) {
        this.request = request;
    }

    @Override
    public LeaveRequest getRequest() {
        return this.request;
    }

    @Override
    public IChainHandler<LeaveRequest> build() {
        // 请假天数：最小0.5天
        double days = getRequest().getDays();
        IChainHandler<LeaveRequest> start = new LeaveStartHandler();
        start.setNext(new TeamLeaderHandler());//组长必须审批
        IChainHandler<LeaveRequest> handler = start.getNext();
        if (days > 2) {//请假天数2-5天，部门经理必须审批
            handler = handler.setNext(new DepartmentManagerHandler());
        }
        if (days > 5) {//请假天数5天以上，总经理必须审批
            handler = handler.setNext(new GeneralManagerHandler());
        }
        handler.setNext(new LeaveEndHandler());
        return start;
    }

}
